package com.tender.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.tender.domain.ConfigInfo;
import com.tender.domain.ConfigInfoCondition;
import com.tender.domain.ConfigInfoExample;
import com.tender.mapper.ConfigInfoMapper;
import com.tender.service.ConfigInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @DS("slave_1") 指定使用 slave_1 数据源
 * @DS("master") 指定使用 master 数据源，默认就是master 数据源
 * @see DS
 */
//@DS("master")
//@DS("slave_1")
@Slf4j
@Service
public class ConfigInfoServiceImpl implements ConfigInfoService {

    @Autowired
    private ConfigInfoMapper configInfoMapper;

    @Override
    public List<ConfigInfo> selectList(ConfigInfoCondition param) {

        ConfigInfoExample example = new ConfigInfoExample();
        ConfigInfoExample.Criteria criteria = example.createCriteria();
        if (!CollectionUtils.isEmpty(param.getDataIdList())) {
            criteria.andDataIdIn(param.getDataIdList());
        }
        if (!CollectionUtils.isEmpty(param.getGroupIdList())) {
            criteria.andGroupIdIn(param.getGroupIdList());
        }

        List<ConfigInfo> configInfos = configInfoMapper.selectByExample(example);
        System.out.println("==============> query from db info : " + JSON.toJSONString(configInfos));

        return configInfos;
    }

    @Override
    public List<ConfigInfo> selectListByManual(ConfigInfoCondition param) {

        ConfigInfoExample example = new ConfigInfoExample();
        ConfigInfoExample.Criteria criteria = example.createCriteria();
        if (!CollectionUtils.isEmpty(param.getDataIdList())) {
            criteria.andDataIdIn(param.getDataIdList());
        }
        if (!CollectionUtils.isEmpty(param.getGroupIdList())) {
            criteria.andGroupIdIn(param.getGroupIdList());
        }

        /**
         * 1、通过线程上下文来设置当前线程数据源 如非必要不要手动调用，调用后确保最终清除
         * DynamicDataSourceContextHolder.push("slave_1");
         * 2、获得当前线程数据源
         * DynamicDataSourceContextHolder.peek();
         * 3、强制清空本地线程 防止内存泄漏，如手动调用了 push 可调用此方法确保清除
         * DynamicDataSourceContextHolder.poll();
         */
        List<ConfigInfo> configInfos = new ArrayList<>();

        try {
            DynamicDataSourceContextHolder.push("slave_1");
            //查看当前数据源
            log.info(DynamicDataSourceContextHolder.peek());
            configInfos.addAll(configInfoMapper.selectByExample(example));
        } finally {
            DynamicDataSourceContextHolder.poll();
        }

        try {
            DynamicDataSourceContextHolder.push("master");
            //查看当前数据源
            log.info(DynamicDataSourceContextHolder.peek());
            configInfos.addAll(configInfoMapper.selectByExample(example));
        } finally {
            DynamicDataSourceContextHolder.poll();
        }

        System.out.println("==============> query from db info : " + JSON.toJSONString(configInfos));

        return configInfos;
    }

}
